package com.epfresh.supplier.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.AuthEntity;

import java.util.ArrayList;

/**
 * Created by shenjie on 2017/9/14.
 */

public class AuthAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<AuthEntity> list;
    private SelectAllImageStatusListener listener;

    public AuthAdapter(Context context, ArrayList<AuthEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if(convertView==null){
            holder=new ViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.list_auth_set,null);
            holder.line=convertView.findViewById(R.id.line);
            holder.menuExplode= (ImageView) convertView.findViewById(R.id.left_iv);
            holder.mainTitle= (TextView) convertView.findViewById(R.id.main_tv);
            holder.subTitle= (TextView) convertView.findViewById(R.id.sub_tv);
            holder.switchIV= (ImageView) convertView.findViewById(R.id.set_iv);
            convertView.setTag(holder);
            holder.container= (LinearLayout) convertView.findViewById(R.id.container);
        }
        holder= (ViewHolder) convertView.getTag();
        holder.switchIV.setTag(position);
        holder.menuExplode.setTag(position);
        AuthEntity item=list.get(position);
        if(item.getMenu()==1||item.isVisible()){
            if(convertView.getVisibility() == View.GONE) {
                convertView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
                convertView.setVisibility(View.VISIBLE);
            }
        }else {
            convertView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,1));
            convertView.setVisibility(View.GONE);
            return convertView;
        }
        if(item.getMenu()==1&&item.isVisible()){
            holder.menuExplode.setImageResource(R.mipmap.up);
        }
        if(item.getMenu()==1&&!item.isVisible()){
            holder.menuExplode.setImageResource(R.mipmap.bottom);
        }

        if(item.getMenu()==1){
            holder.line.getLayoutParams().height=15;
            holder.menuExplode.setVisibility(View.VISIBLE);
            holder.subTitle.setVisibility(View.GONE);
            holder.menuExplode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int p= (int) v.getTag();
                    AuthEntity entity=list.get(p);
                    if(entity.getMenu()==1&&entity.isVisible()){
                        for (AuthEntity item:list){
                            if(entity.getId().equals(item.getParentId())){
                                item.setVisible(false);
                            }
                        }
                    }else if(entity.getMenu()==1&&!entity.isVisible()){
                        for (AuthEntity item:list){
                            if(entity.getId().equals(item.getParentId())){
                                item.setVisible(true);
                            }
                        }
                    }
                    entity.setVisible(!entity.isVisible());
                    notifyDataSetChanged();
                }
            });


        }else {
            holder.line.getLayoutParams().height=1;
            holder.menuExplode.setVisibility(View.INVISIBLE);
            holder.subTitle.setVisibility(View.VISIBLE);
            holder.subTitle.setText(item.getSubTitle());

        }
        if(item.isHasAuth()){
            holder.switchIV.setImageResource(R.mipmap.switchon);
        }else {
            holder.switchIV.setImageResource(R.mipmap.switchoff);
        }

        holder.switchIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int click= (int) v.getTag();

                AuthEntity select=list.get(click);
                if(select.isHasAuth()){
                    select.setHasAuth(false);
                    if(select.getMenu()==1){
                        for (AuthEntity item:list){
                            if(select.getId().equals(item.getParentId())){
                                item.setHasAuth(false);
                            }
                        }
                    }else {
                        for (AuthEntity item:list){
                            if(select.getParentId().equals(item.getId())){
                                item.setHasAuth(false);
                            }
                        }
                    }

                }else {
                    select.setHasAuth(true);
                    if(select.getMenu()==1){
                        for (AuthEntity item:list){
                            if(select.getId().equals(item.getParentId())){
                                item.setHasAuth(true);
                            }
                        }
                    }else {
                        boolean canSet=true;
                        AuthEntity changeItem=null;
                        for (AuthEntity item:list){
                            if(select.getParentId().equals(item.getId())){
                                changeItem=item;
                            }
                            if(select.getParentId().equals(item.getParentId())){
                                if(!item.isHasAuth()){
                                    canSet=false;
                                    break;
                                }
                            }
                        }
                        if(changeItem!=null){
                            changeItem.setHasAuth(canSet);
                        }
                    }

                }
                notifyDataSetChanged();
                if(listener!=null){
                    listener.changeStatus();
                }
            }
        });
        holder.mainTitle.setText(item.getMainTitle());



        return convertView;
    }

    private class ViewHolder{
        LinearLayout container;
        View line;
        ImageView menuExplode;
        TextView mainTitle,subTitle;
        ImageView switchIV;
    }

    public ArrayList<AuthEntity> getList() {
        return list;
    }

    public void setList(ArrayList<AuthEntity> list) {
        this.list = list;
    }

    public  interface SelectAllImageStatusListener{
        public void changeStatus();
    }

    public SelectAllImageStatusListener getListener() {
        return listener;
    }

    public void setListener(SelectAllImageStatusListener listener) {
        this.listener = listener;
    }
}




